package com.jialea.springboot.common.result;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回前端实体类
 */
@ApiOperation(value = "响应实体类")
@Data
public class Result<T> implements Serializable {
    @ApiModelProperty("响应码")
    private Integer code;
    @ApiModelProperty("成功判断")
    private Boolean success;
    @ApiModelProperty("响应消息")
    private String msg;
    @ApiModelProperty("响应数据")
    private T data;

    /**
     * 初始化成功
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> initSuccess() {
        Result<T> success = new Result<>();
        success.setSuccess(true);
        return success;
    }

    /**
     * 初始化错误
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> initError() {
        Result<T> r = new Result<>();
        r.setSuccess(false);
        return r;
    }

    /**
     * 操作成功，无数据返回
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success() {
        Result<T> success = initSuccess();
        ResultCodeEnum codeEnum = ResultCodeEnum.SUCCESS;
        success.setCode(codeEnum.getCode());
        success.setMsg(codeEnum.getMsg());
        return success;
    }

    /**
     * 操作成功，返回数据
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result<T> success = initSuccess();
        ResultCodeEnum codeEnum = ResultCodeEnum.SUCCESS;
        success.setCode(codeEnum.getCode());
        success.setMsg(codeEnum.getMsg());
        success.setData(data);
        return success;
    }

    /**
     * 操作失败
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> fail() {
        Result<T> error = initError();
        ResultCodeEnum codeEnum = ResultCodeEnum.ERROR;
        error.setCode(codeEnum.getCode());
        error.setMsg(codeEnum.getMsg());
        return error;
    }

    /**
     * 操作失败返回自定义错误信息
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> fail(String msg) {
        Result<T> error = initError();
        ResultCodeEnum codeEnum = ResultCodeEnum.ERROR;
        error.setCode(codeEnum.getCode());
        error.setMsg(msg);
        return error;
    }

    /**
     * 操作失败返回自定义编码和错误信息
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> fail(Integer code, String msg) {
        Result<T> error = initError();
        error.setCode(code);
        error.setMsg(msg);
        return error;
    }

    /**
     * 操作失败返回枚举
     *
     * @param resultCodeEnum
     * @param <T>
     * @return Result
     */
    public static <T> Result<T> fail(ResultCodeEnum resultCodeEnum) {
        Result<T> error = new Result<>();
        error.setSuccess(false);
        error.setCode(resultCodeEnum.getCode());
        error.setMsg(resultCodeEnum.getMsg());
        return error;
    }


}
